//
//  Inquire_ViewController.m
//  EggSteward
//
//  Created by 王道林 on 16/9/7.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "Inquire_ViewController.h"
#import "DLWNetworkingParser.h"
#import "BaseLabel.h"
#import "DLMacro.h"
#import "Masonry.h"
#import "BaseView.h"
#import "InquireView.h"
#import "BaseTableView.h"
#import "Inquire_TabelViewCell.h"
#import "InquireModel.h"

@interface Inquire_ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrTable;
@property (nonatomic, strong) NSMutableArray *arrView;
@end

@implementation Inquire_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self handleData];
    self.navigationItem.title = @"查询统计";
    [self createSubViewHeader];
    
}
#pragma mark - 处理数据
- (void)handleData
{
    _arrView = [NSMutableArray array];
    _arrTable = [NSMutableArray array];
    [DLWNetworkingParser POSTWithURL:@"http://www.danjiguanjia.com/api/Price/eggSortPrice" par:nil success:^(id responseObject) {
        // tableview的数据
        for (NSDictionary *dic in responseObject[@"data"][@"province"][@"data"]) {
            InquireModel *model = [InquireModel modelWithDic:dic];
            [_arrTable addObject:model];
        }
        [_tableView reloadData];
        // 上面的数据
        InquireModel *modelO = [InquireModel modelWithDic:responseObject[@"data"][@"province"][@"first"]];
        [_arrView addObject:modelO];
        InquireModel *modelT = [InquireModel modelWithDic:responseObject[@"data"][@"province"][@"end"]];
        [_arrView addObject:modelT];
        InquireModel *modelS = [InquireModel modelWithDic:responseObject[@"data"][@"city"][@"first"]];
        [_arrView addObject:modelS];
        InquireModel *modelF = [InquireModel modelWithDic:responseObject[@"data"][@"city"][@"end"]];
        [_arrView addObject:modelF];

        [self createSubViewBody];
        
    } filed:^(NSError *error) {
        
    }];
}


#pragma mark - 创建视图
- (void)createSubViewHeader
{
    BaseLabel *lableheader = [[BaseLabel alloc] init];
    lableheader.text = @"昨日蛋价排行";
    lableheader.font = [UIFont systemFontOfSize:25];
    lableheader.textAlignment = 1;
    lableheader.clipsToBounds = YES;
    lableheader.shadowColor = [UIColor grayColor];
    lableheader.shadowOffset = CGSizeMake(1, 2);
    [self.view addSubview:lableheader];
    [lableheader mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top);
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.height.mas_equalTo(60);
    }];
    // 线
    UIView *viewLine = [[UIView alloc] init];
    viewLine.backgroundColor = [UIColor grayColor];
    [self.view addSubview:viewLine];
    [viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lableheader.mas_bottom);
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.height.mas_equalTo(1);
    }];
    
    // 绿条
    BaseView *viewGreen = [[BaseView alloc] init];
    viewGreen.backgroundColor = [UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000];
    [[NSNotificationCenter defaultCenter] removeObserver:viewGreen name:@"night" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:viewGreen name:@"day" object:nil];

    [self.view addSubview:viewGreen];
    [viewGreen mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).mas_offset(290);
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.height.mas_equalTo(40);
    }];
    
    
    BaseLabel *lableRanging = [[BaseLabel alloc] init];
    lableRanging.text = @"排行";
    lableRanging.textColor = [UIColor whiteColor];
    [viewGreen addSubview:lableRanging];
    [lableRanging mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewGreen.mas_top);
        make.left.mas_equalTo(viewGreen.mas_left).mas_offset(25);
        make.width.mas_equalTo(viewGreen.mas_width).multipliedBy(0.333).mas_offset(-25);
        make.height.mas_equalTo(viewGreen.mas_height);
    }];

    BaseLabel *lableProvince = [[BaseLabel alloc] init];
    lableProvince.text = @"省份";
    lableProvince.textColor = [UIColor whiteColor];
    [viewGreen addSubview:lableProvince];
    [lableProvince mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewGreen.mas_top);
        make.left.mas_equalTo(lableRanging.mas_right);
        make.width.mas_equalTo(viewGreen.mas_width).multipliedBy(0.333);
        make.height.mas_equalTo(viewGreen.mas_height);
    }];

    BaseLabel *lablePrice = [[BaseLabel alloc] init];
    lablePrice.text = @"价格(元/斤)";
    lablePrice.textColor = [UIColor whiteColor];
    [viewGreen addSubview:lablePrice];
    [lablePrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewGreen.mas_top);
        make.left.mas_equalTo(lableProvince.mas_right);
        make.width.mas_equalTo(viewGreen.mas_width).multipliedBy(0.333);
        make.height.mas_equalTo(viewGreen.mas_height);
    }];

    // 创建tableView
    _tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 330, VIEW_WIDTH, self.view.frame.size.height - 330 - 64 - 49) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.bounces = NO;
    [self.view addSubview:_tableView];
    // 注册
    [_tableView registerClass:[Inquire_TabelViewCell class] forCellReuseIdentifier:@"reuse"];
}
#pragma mark - tableView的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrTable.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Inquire_TabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.lableRanging.text = [NSString stringWithFormat:@"%ld", (indexPath.row + 1)];
    cell.model = _arrTable[indexPath.row];
    return cell;
}


- (void)createSubViewBody
{
    InquireModel *modelO = _arrView[0];
    NSString *stringO = [NSString stringWithFormat:@"%@元/斤", modelO.value];
    InquireView *ViewOne = [[InquireView alloc] initWithFrame:CGRectMake(0, 60, VIEW_WIDTH / 2, 110) title:@"最高的省份" place:modelO.name Price:stringO];
    [self.view addSubview: ViewOne];
    
    InquireModel *modelT = _arrView[1];
    NSString *stringT = [NSString stringWithFormat:@"%@元/斤", modelT.value];
    InquireView *ViewTwo = [[InquireView alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 2, 60, VIEW_WIDTH / 2, 110) title:@"最低的省份" place:modelT.name Price:stringT];
    [self.view addSubview: ViewTwo];
    
    InquireModel *modelS = _arrView[2];
    NSString *strings = [modelS.value substringToIndex:4];
    NSString *stringS = [NSString stringWithFormat:@"%@元/斤", strings];
    InquireView *ViewThree = [[InquireView alloc] initWithFrame:CGRectMake(0, 170, VIEW_WIDTH / 2, 110) title:@"最高的城市" place:modelS.name Price:stringS];
    [self.view addSubview: ViewThree];
    
    InquireModel *modelF = _arrView[3];
//    NSString *stringf = [modelF.value substringToIndex:3];
    NSString *stringF = [NSString stringWithFormat:@"%@元/斤", modelF];
    InquireView *ViewFour = [[InquireView alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 2, 170, VIEW_WIDTH / 2, 110) title:@"最低的城市" place:modelF.name Price:stringF];
    [self.view addSubview: ViewFour];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
